<?PHP
// PHP World of Warcraft level up and down script
// Created by Gachl (Daniel Vogel)

include_once "configa.php";

// Change following:
$config = "scripts.inc.php";
$minlevel = 1;
$maxleveldistance = 90;
$downleveling = true;
$usingtype = "n";
$configfilename = "config_levels.cfg";

// Edit the following function that it returns the amount in copper (1 gold = 10'000 copper)
// Default value: a lot.
// Parameter: $currentLevel: The players current level
//            $targetLevel: The players target level
function fCalculateLevelingCosts($currentLevel, $targetLevel) {
	$costs = (round(pow($targetLevel, 2.25),2) - round(pow($currentLevel, 2),2) - round(($targetLevel - $currentLevel)*10,2)) * 0;
	if ($costs < 0)
		$costs = $costs * (-1);
	return $costs;
}

// ########################
// #Don't change from here#
// ########################
require_once($config);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title><?= $websitetitle ?></title>
</head>
<body>
  
  <?PHP
// Script
$wuser = fGetUser($userid);
if ($wuser === false)
  die("The user with the user id " . $userid . " does not exists!" . ($userid == 0 ? " Website admin please set up the current user id settings!" : ''));

// Load config file
// This code is stupid <.<
if ($usingtype != "n") {
	$conffile = "";
	do {
		if (!file_exists($configfilename)) {
			if (!is_writable(dirname($configfilename))) {
				die("Please create a file with the name &quot;$configfilename&quot; and make it writable (chmod 777).");
			}
			$fh = fopen($configfilename, 'w');
			fwrite($fh, $usingtype . "\n");
			fclose($fh);
		}
		$fh = fopen($configfilename, "r");
		while (!feof($fh))
			$conffile = fread($fh, 1024);
		fclose($fh);
		if (trim($conffile) != "") {
			$conffile = split("\n", $conffile);
			$config['usingtype'] = $conffile[0];
		} else {
			$config['usingtype'] = "n";
		}
		if ($config['usingtype'] != $usingtype) {
			$fh = fopen($configfilename, "w");
			fwrite($fh, $usingtype);
			fclose($fh);
		}
	} while ($config['usingtype'] != $usingtype);
	unset($conffile[0]);
	foreach ($conffile as $confline) {
		$confline = split(";", $confline);
		$config[$confline[0]] = $confline[1];
	}

	if ($usingtype == "e") {
		$now = time();
		$sub = (intval($now) - (intval($config[1]) + ($usingspan * 60)));
		if (!empty($config[1]) && ($sub < 0))
			die("This tool cannot be used at the moment. You have to wait " . round($sub * (-1) / 60, 1) . " minute(s).");
	}
	if ($usingtype == "a") {
		$now = time();
		$sub = (intval($now) - (intval($config[$userid]) + ($usingspan * 60)));
		if (!empty($config[$userid]) && ($sub < 0))
			die("You can't use this tool. You have to wait " . round($sub * (-1) / 60, 1) . " minute(s).");
	}
}

if (!empty($_POST['cid']) && !empty($_POST['lvl'])) {
	// Do porting here
	
	$charid = $_POST['cid'];
	if ($usingtype == "c") {
		$now = time();
		$sub = (intval($now) - (intval($config[$charid]) + ($usingspan * 60)));
		if (!empty($config[$charid]) && ($sub < 0))
			die("You can't use this tool with this character. You have to wait " . round($sub * (-1) / 60, 1) . " minute(s).");
	}
	$res = fMySQL_query('SELECT * FROM `characters` WHERE `guid` = '.$charid.';', DB_ACCOUNT);
	if (!($res = mysql_fetch_assoc($res))) {
		die('This character does not exists.');
	}
	if ($res['acct'] != $wuser['acct']) {
		die('This is not your character.');
		break;
	}
	$tlevel = intval($_POST['lvl']);
	$clevel = intval($res['level']);
	if (!$downleveling) {
		if ($tlevel <= $clevel)
			die("Your target level must be higher than $clevel!");
	}
	if ($tlevel > $maxlevel)
		die("The highest level is $maxlevel!");
	if ($tlevel < $minlevel)
		die("The smallest level is $minlevel!");
	if ((($tlevel - $clevel) > $maxleveldistance) || (($clevel - $tlevel) > $maxleveldistance))
		die("The max. distance between two levels is $maxleveldistance.");
	$kosten = fCalculateLevelingCosts($clevel, $tlevel);
	if (empty($_POST['accept'])) {
		if ($res['gold'] < $kosten) {
			$ch = intval(substr($kosten, -2));
			$si = intval(substr(round(intval($kosten) / 100), -2));
			$go = intval(round(intval($kosten) / 10000));
			die('To level you up to the level '.$tlevel.' you need '."$go Gold, $si Silver and $ch Copper. You have not that much.");
		}
		$ch = intval(substr($kosten, -2));
		$si = intval(substr(round(intval($kosten) / 100), -2));
		$go = intval(round(intval($kosten) / 10000));
		echo "To level " . $res['name'] . " up from level $clevel to level $tlevel costs you $go gold, $si silver und $ch copper. Are you sure you want that?";
		echo "<br>\n";
		echo '<form action="?" method="POST"><input type="hidden" name="cid" value="'.$charid.'"><input type="hidden" name="lvl" value="'.$tlevel.'"><input type="submit" name="accept" value="YES!"></form>';
		echo $content;
		exit();
	}
	$newgold = $res['gold'] - $kosten;
	if ($res['online'] != "0") {
		die('This character is currently online. Please log out before you level up.');
	}
	
	fMySQL_query('UPDATE `characters` SET `gold` = \''.$newgold.'\' WHERE `guid` = '.$charid.';', DB_ACCOUNT);
	fMySQL_query('UPDATE `characters` SET `level` = '.$tlevel.' WHERE `guid` = '.$charid.';', DB_ACCOUNT);
	// Save config file
		if ($usingtype != "n") {
			$fh = fopen($configfilename, "w");
			fwrite($fh, $usingtype . "\n");
			switch ($usingtype) {
			case "a":
				$config[$userid] = time();
				break;
			case "c":
				$config[$charid] = time();
				break;
			case "e":
				$config[1] = time();
				break;
			}
			foreach ($config as $id => $time) {
				if (($id != "usingtype") && !empty($id)) {
					fwrite($fh, $id . ";" . $time . "\n");
				}
			}
			fclose($fh);
		}
	echo "Your character is now level $tlevel!";
}

echo '<form action="?" method="POST"><table><tr><td>Character:&nbsp;</td><td><select name="cid">';
$sql = 'SELECT * FROM `characters` WHERE `acct` = ' . $wuser['acct'] . ';';
$res = fMySQL_query($sql, DB_ACCOUNT);
while ($row = mysql_fetch_assoc($res))
	echo '<option value="'.$row['guid'].'">'.$row['name'].' ('.$row['level'].')</option>'."\n";
echo '</select></td></tr><tr><td>target level:&nbsp;</td><td><select name="lvl">';
for ($i = 0; $i < 90; $i++) {
	echo '<option value="'.($i+1).'">'.($i+1).'</option>'."\n";
}
echo '</select></td><tr><td></td><td><input type="submit" value="Next"></td></tr></table></form>';
?>
</body>
</html>
<!-- This script is written by Gachl (Daniel Vogel). http://codefreak.net http://data-universal.net http://blog.codefreak.net Queries: <?= $qcount ?> -->